package com.example.ivory.accountbook.Activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ivory.accountbook.R;
import com.example.ivory.accountbook.uilt.TittleManager;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Ivory on 2016/8/29.
 */
public class Sign extends Activity implements View.OnClickListener {
    private TittleManager tittleManager;
    private EditText number;
    private EditText pwd;
    private EditText rePwd;
    private EditText code;
    String user_number,user_pwd,user_rePwd,user_code;
    private Button resent,finish_sign;

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_sign);
       tittleManager=new TittleManager(this);
       tittleManager.setTitleStyle(TittleManager.TitleStyle.ONLY_BACK,"新用户注册");

        initView();
        initEvent();
   }
    public void initView(){
        number=(EditText)findViewById(R.id.number);
        pwd=(EditText)findViewById(R.id.pwd);
        rePwd=(EditText)findViewById(R.id.rePwd);
        resent=(Button)findViewById(R.id.resent);
        finish_sign=(Button)findViewById(R.id.finish_sign);
        code=(EditText)findViewById(R.id.code);

    }
    public void initEvent(){
        finish_sign.setOnClickListener(this);
        resent.setOnClickListener(this);
    }

    public static boolean isMobileNumberValid(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(17[1,3,5-8])|(14[5,7,9])|(18[0,2-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public boolean check(){
        user_code=code.getText().toString();
        user_number = number.getText().toString();
        user_pwd = pwd.getText().toString();
        user_rePwd = rePwd.getText().toString();
        if(!isMobileNumberValid(user_number)){
            Toast.makeText(Sign.this, "手机号码错误", Toast.LENGTH_LONG).show();
            return false;
        }else if(!user_code.equals("1234")){
            Toast.makeText(Sign.this, "验证码错误", Toast.LENGTH_LONG).show();
            return false;
        }
         if(user_pwd.equals("")){
            Toast.makeText(Sign.this, "密码不能为空", Toast.LENGTH_LONG).show();
            return false;
        }
        if (!user_pwd.equals(user_rePwd)) {
            Toast.makeText(Sign.this, "密码不一致", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.finish_sign:
                if(check()){
                    Toast.makeText(Sign.this, "注册成功", Toast.LENGTH_LONG).show();
                    finish();
                }
                break;
            case R.id.resent:
                //重新发送验证码
                break;
        }
    }
}
